/*
 *  glfuncs.cpp
 *  framework
 *
 *  Created by gerhard gubler on 17.11.09.
 *  Copyright 2009 md80.ch . All rights reserved.
 *
 */

#include "glfuncs.h"

using std::string;

glfuncs::glfuncs()
{
	rotx = 0;
	txtrot = 0;
	intcount = 0;
	hundred = (char*) malloc(char(3));
	ten = (char*) malloc(char(3));
	one = (char*) malloc(char(3));
	one_up = (char*) malloc(char(3));
	one_down = (char*) malloc(char(3));
	memset(hundred, 0, 3);
	memset(ten, 0, 3);
	memset(one,0, 3);
	memset(one_up,0, 3);
	memset(one_down,0, 3);
							 
}

void glfuncs::line(GLfloat posx, GLfloat posy, GLfloat posz, float value)
{
	glPushMatrix();
	glLoadIdentity();
	glTranslatef(posx, posy, posz);
	glRotatef(-value, 0.0, 0.0, 1.0);
	glColor3f(0.0, 1.0, 0.0);
	glBegin(GL_LINES);
	glVertex3f(-55.0, 0.0, 0.0);
	//glVertex3f(0.0, 0.0, 0.0);
	glVertex3f(-65.0, 0.0, 0.0);
	glEnd();
	glPopMatrix();
}

void glfuncs::frame1(GLfloat posx, GLfloat posy, GLfloat posz)
{
	glLoadIdentity();
	glPushMatrix();
	//glColor3f(0.0, 0.5, 1.0);
	glColor3f(0.0, 0.0, 0.0);
	glTranslatef(posx, posy, posz);
	glBegin(GL_LINES);
	glVertex3f(0.0,  0.0, 0.0);
	glVertex3f(25.0, 0.0, 0.0);
	glVertex3f(25.0, 0.0, 0.0);
	glVertex3f(25.0, -10.0, 0.0);
	glVertex3f(25.0, -10.0, 0.0);
	glVertex3f(40.0, -10.0, 0.0);
	glEnd();
	glBegin(GL_LINES);
	glVertex3f(0.0,  20.0, 0.0);
	glVertex3f(25.0, 20.0, 0.0);
	glVertex3f(25.0, 20.0, 0.0);
	glVertex3f(25.0, 30.0, 0.0);
	glVertex3f(25.0, 30.0, 0.0);
	glVertex3f(40.0, 30.0, 0.0);
	glEnd();
	glPopMatrix();
}

void glfuncs::box2(GLfloat posx, GLfloat posy, GLfloat posz)
{
	
	glLoadIdentity();
	glPushMatrix();
	glColor3f(1.0, 0.0, 0.0);
	glTranslatef(posx, posy, posz);
	glBegin(GL_POLYGON);
	glVertex3f(0.0, 0.0, 0.0);
	glVertex3f(50.0, 0.0, 0.0);
	glVertex3f(50.0, 40.0, 0.0);
	glVertex3f(0.0, 40.0, 0.0);
	glEnd();
	glPopMatrix();
}

void glfuncs::box3(GLfloat posx, GLfloat posy, GLfloat posx1, GLfloat posy1)
{
	
	glLoadIdentity();
	glColor3f(0.0,0.0,0.0);	
	glTranslatef(posx, posy, 0.0);
	glRectf(0.0, 0.0, posx1, posy1);
	
}

void glfuncs::text(GLfloat posx, GLfloat posy, string input)
{
	glColor3f(1.0, 1.0, 1.0);
	glLoadIdentity();
	glPushMatrix();
	glRasterPos3f(posx, posy,0.0);
	const char *txt = input.c_str();
	for (const char *loopa = txt; *loopa != '\0'; loopa++)
	{
		glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24 , *loopa );
	}
	glPopMatrix();
}

void glfuncs::text2(GLfloat posx, GLfloat posy, string input)
{
	glColor3f(GLBLUE);
	glLoadIdentity();
	glPushMatrix();
	glRasterPos3f(posx, posy,0.0);
	const char *txt = input.c_str();
	for (const char *loopa = txt; *loopa != '\0'; loopa++)
	{
		glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_10 , *loopa );
	}
	glPopMatrix();
}

void glfuncs::flush()
{
	glFlush();
}

void glfuncs::call_vsc(GLfloat posx, GLfloat posy, float val)
{
	myvsc.display(posx, posy, val);
}

void glfuncs::test()
{
	glLoadIdentity();
	glColor3f(GLRED);	
	glTranslatef(200.0, 200.0, 0.0);
	glRectf(100.0, 200.0, 30.0, 20.0);

}

bool glfuncs::load_rgb_image(const char* file_name, int w, int h, RGBIMG* refimg)
{
	FILE*    file;  // The Image's File On Disk
	long     fsize; // File Size In Bytes
	GLubyte* p;     // Helper Pointer
	
	// Open The File And Read The Pixels
	file = fopen(file_name , "rb");
	if (!file) return false;
	fseek(file, 0L, SEEK_END);
	fsize = ftell(file);

	fseek(file, 0L, SEEK_SET);
	refimg->data = new GLubyte [fsize];
	p = refimg->data;
	if (refimg->data == NULL) return false;
	fread(p, sizeof(char), fsize, file);
	fclose(file); 
	return true;
}

void glfuncs::set_num_textures(int num)
{
	// Create The Textures' Id List
	glGenTextures(num, g_texid);
}

// Setup Our Textures. Returns true On Success, false On Fail
bool glfuncs::setup_textures(int sw, int sh, int num)
{
	RGBIMG img;

	// Load The Image From A Disk File
	if (!load_rgb_image(text_name.c_str(), sw, sh, &img)) return false;
	// Typical Texture Generation Using Data From The Image
	glBindTexture(GL_TEXTURE_2D, g_texid[num-1]);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
	
	glTexImage2D(GL_TEXTURE_2D, 0, 3, sw, sh, 0, GL_BGR, GL_UNSIGNED_BYTE, img.data);
	// Finished With Our Image, Free The Allocated Data
	delete img.data;
	return true;
}

void glfuncs::showtexture(GLfloat posx, GLfloat posy, int num)
{
	glEnable(GL_TEXTURE_2D); 
	glLoadIdentity();
	glColor3f(1.0, 1.0, 1.0);
	glBindTexture(GL_TEXTURE_2D, g_texid[num]);
	glPushMatrix();
	glTranslatef(posx, posy, 0.0);
	glBegin(GL_QUADS);
	// Front Face
	glTexCoord2f(0.08f, 0.0f); glVertex3f(-50.0f, -50.0f,  0.0f);
	glTexCoord2f(1.08f, 0.0f); glVertex3f( 50.0f, -50.0f,  0.0f);
	glTexCoord2f(1.08f, 1.0f); glVertex3f( 50.0f,  50.0f,  0.0f);
	glTexCoord2f(0.05f, 1.0f); glVertex3f(-50.0f,  50.0f,  0.0f);
	glEnd();
	glPopMatrix();
	glDisable(GL_TEXTURE_2D); 
}

void glfuncs::set_Texture_name(string fname)
{
	text_name = fname;
}
